package datastream;

import java.io.*;

public class DataInputStreamDemo {

    public static void main(String[] args) {
        File destFile = new File("d:" + File.separator + "dest.data");
        DataInputStream din = null;
        DataOutputStream dout = null;
        try {

            dout = new DataOutputStream(new FileOutputStream(destFile));
            dout.writeInt(100); //写入整数
            dout.writeDouble(Math.PI); //写入浮点数
            dout.writeBoolean(true); //写入布尔
            dout.writeChar('中'); //写入字符
            dout.writeUTF("hello,world!"); //写入字符串

            dout.flush();

            din = new DataInputStream(new FileInputStream(destFile));
            //注意：读取类型顺序必须和写入类型顺序一致。
            System.out.println(din.readInt()); //读取整数
            System.out.println(din.readDouble()); //读取浮点数
            System.out.println(din.readBoolean()); //读取布尔
            System.out.println(din.readChar()); //读取字符
            System.out.println(din.readUTF()); //读取字符串

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (din != null) {
                    din.close();
                    din = null;
                }
                if (dout != null) {
                    dout.close();
                    dout = null;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}
